using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System;
using UnityEngine.SceneManagement;
using static QFramework.Adventure.GameData;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace QFramework.Adventure
{
	public class MainMenuPanelData : UIPanelData
	{
	}
	public partial class MainMenuPanel : UIPanel, IController
    {
      

        protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as MainMenuPanelData ?? new MainMenuPanelData();
			// please add init code here		
		}
		
		protected override void OnOpen(IUIData uiData = null)
		{
            this.GetUtility<IStorage>().LoadData();

            UIEventTrigger.Get(btn_newGame).onClick = NewGameFunction;
			UIEventTrigger.Get(btn_continue).onClick = ContinueFunction;
			UIEventTrigger.Get(btn_quit).onClick = QuitFunction;
			UIEventTrigger.Get(btn_detail).onClick = DetailFunction;

        }

	

		protected override void OnShow()
		{
		}
		
		protected override void OnHide()
		{
		}
		
		protected override void OnClose()
		{
        

            UIEventTrigger.Get(btn_newGame).onClick = null;
            UIEventTrigger.Get(btn_continue).onClick = null;
            UIEventTrigger.Get(btn_quit).onClick = null;
            UIEventTrigger.Get(btn_detail).onClick = null;

        }

        //============================>

        private void DetailFunction(GameObject go)
        {
            AudioKit.PlaySound(GameConfig.Auido_Btn01);
            UIKit.OpenPanel<GameDetailPanel>();
        }
        private void QuitFunction(GameObject go)
        {
            AudioKit.PlaySound(GameConfig.Auido_Btn01);
            CloseSelf();

#if UNITY_EDITOR
            EditorApplication.isPlaying = false;

#elif UNITY_ANDROID||UNITY_STANDALONE_WIN
			Application.Quit();
#endif
        }

        private void ContinueFunction(GameObject go)
        {
            if (GameData.storageData.currentAddSceneName== null) { NewGameFunction(btn_newGame.gameObject); return; }

            AudioKit.PlaySound(GameConfig.Auido_Btn01);
            UIKit.OpenPanel<LoadingPanel>(new LoadingPanelData() { sceneName = GameData.storageData.currentAddSceneName }) ;
            GameData.loadState = LoadState.CONTINUE;
            CloseSelf();
        }

        private void NewGameFunction(GameObject go)
        {
            AudioKit.PlaySound(GameConfig.Auido_Btn01);
            this.GetUtility<IStorage>().ClearData();
            UIKit.OpenPanel<LoadingPanel>(new LoadingPanelData() { sceneName = QAssetBundle.Level01_unity.LEVEL01}) ;
            GameData.loadState = LoadState.NEWGAME;

            CloseSelf();
        }
        public IArchitecture GetArchitecture()
        {
            return AdventureApp.Interface;
        }
    }
}
